package com.ptszyxx.popose.module.main.mine;

import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;

import com.github.gzuliyujiang.wheelpicker.OptionPicker;
import com.github.gzuliyujiang.wheelpicker.contract.OnOptionPickedListener;
import com.ptszyxx.popose.BR;
import com.ptszyxx.popose.R;
import com.ptszyxx.popose.databinding.FragmentMinePriceBinding;
import com.ptszyxx.popose.module.main.mine.vm.MinePriceVM;
import com.ysg.base.BaseFragment;
import com.ysg.http.data.AppViewModelFactory;
import com.ysg.http.data.entity.mine.PriceEntity;
import com.ysg.http.data.entity.user.UserResult;
import com.ysg.utils.YStringUtil;
import com.ysg.utils.YToastUtil;

import java.util.ArrayList;
import java.util.List;

public class MinePriceFragment extends BaseFragment<FragmentMinePriceBinding, MinePriceVM> {
    public static MinePriceFragment getInstance() {
        return new MinePriceFragment();
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_mine_price;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public MinePriceVM initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(MinePriceVM.class);
    }

    @Override
    public void initData() {
        super.initData();
        viewModel.requestPriceInfo();
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.uc.onUIEvent.observe(this, o -> setUI());
        viewModel.uc.onPriceEvent.observe(this, o -> showDialogPrice((int) o));
    }

    private void setUI() {
        if (viewModel.priceResult == null || viewModel.priceResult.getUser() == null){
            return;
        }
        try {
            UserResult user = viewModel.priceResult.getUser();
            if (YStringUtil.isEmpty(user.getMessagePrice()) || Double.valueOf(user.getMessagePrice()) <= 0){
                binding.tvMessagePrice.setContent(getResources().getString(R.string.free));
            }else{
                binding.tvMessagePrice.setContent(YStringUtil.removeZero(user.getMessagePrice()) + getResources().getString(R.string.mine_price_diamond));
            }
            if (YStringUtil.isEmpty(user.getVideoPrice()) || Double.valueOf(user.getVideoPrice()) <= 0){
                binding.tvVideoPrice.setContent(getResources().getString(R.string.free));
            }else{
                binding.tvVideoPrice.setContent(YStringUtil.removeZero(user.getVideoPrice()) + getResources().getString(R.string.mine_price_diamond));
            }
            if (YStringUtil.isEmpty(user.getVoicePrice()) || Double.valueOf(user.getVoicePrice()) <= 0){
                binding.tvVoicePrice.setContent(getResources().getString(R.string.free));
            }else{
                binding.tvVoicePrice.setContent(YStringUtil.removeZero(user.getVoicePrice()) + getResources().getString(R.string.mine_price_diamond));
            }
            binding.switchVoice.setSwitchStatus(YStringUtil.eq(1, user.getVoiceStatus())? true: false);
            binding.switchVideo.setSwitchStatus(YStringUtil.eq(1, user.getVideoStatus())? true: false);

            binding.tvHtml.setText(Html.fromHtml(viewModel.priceResult.getExplain()));
        }catch (Exception e){
            e.printStackTrace();
            YToastUtil.showShort(e.getMessage());
        }
    }

    private void showDialogPrice(int type) {
        List<String> list = new ArrayList<>();
        List<PriceEntity> priceList = new ArrayList<>();
        OptionPicker picker = new OptionPicker(getActivity());
        switch (type){
            case 1:
                priceList = viewModel.priceResult.getPriceList().get_$1();
                break;
            case 2:
                priceList = viewModel.priceResult.getPriceList().get_$2();
                break;
            case 3:
                priceList = viewModel.priceResult.getPriceList().get_$3();
                break;
        }
        for (PriceEntity item: priceList) {
            list.add(item.getSurfaceName());
        }
        picker.setData(list);
        List<PriceEntity> finalPriceList = priceList;
        picker.setOnOptionPickedListener(new OnOptionPickedListener() {
            @Override
            public void onOptionPicked(int position, Object item) {
                viewModel.requestPriceSave(type, finalPriceList.get(position).getPrice());
            }
        });
        picker.show();
    }


}
